<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设置支付密码</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">

    <style type="text/css">
        .input {
            display: inline-block;
        }
        input {
            border-top: 1px solid #999;
            border-bottom: 1px solid #999;
            border-left: 1px solid #999;
            width: 45px;
            height: 45px;
            outline:none;
            font-family: inherit;
            font-size: 28px;
            font-weight: inherit;
            text-align: center;
            line-height: 45px;
            color: #c2c2c2;
            background: rgba(255,255,255,0);
        }
    </style>
</head>
<body>
    <div class="input">
        <input type="tel" placeholder="随" maxlength="1">
        <input type="tel" placeholder="机" maxlength="1">
        <input type="tel" placeholder="5" maxlength="1">
        <input type="tel" placeholder="位" maxlength="1">
        <input type="tel" placeholder="数" maxlength="1">
    </div>
    <div><button>确认</button></div>

<script type="text/javascript">
    (function (window, document) {
    var active = 0,
        inputBtn = document.querySelectorAll('input');
    for (var i = 0; i < inputBtn.length; i++) {
        inputBtn[i].addEventListener('click', function () {
            inputBtn[active].focus();
        }, false);
        inputBtn[i].addEventListener('focus', function () {
            this.addEventListener('keydown', listenKeyUp, false);
        }, false);
        inputBtn[i].addEventListener('blur', function () {
            this.removeEventListener('keydown', listenKeyUp, false);
        }, false);
    }
 
    /**
     * 监听键盘的敲击事件
     */
    function listenKeyUp(event) {
        console.log(event)
        if (!isNaN(this.value) && this.value.length != 0) {
            if (active < 4) {
                active += 1;
            }
            inputBtn[active].focus();
        } else if (this.value.length == 0) {
            if (active > 0) {
                active -= 1;
            }
            inputBtn[active].focus();
        }
        // if (event.key == "Backspace") {
        //     inputBtn[active].focus();
        // }
    }
})(window, document);
</script>
</body>
</html>